package parser

import (
	"learngo/crawler/engine"
	"regexp"
)

var (
	cityReg    = regexp.MustCompile(`<a href="(http://album.zhenai.com/u/\w+)"[^>]*>([^<]+)</a>`)
	genderReg  = regexp.MustCompile(`<td width="180"><span class="grayL">性别：</span>([^<]+)士</td>`)
	cityUrlReg = regexp.MustCompile(`href="(http://www.zhenai.com/zhenghun/[^"]+)"`)
)

func ParseCity(contents []byte) engine.ParseResult {
	rs := engine.ParseResult{}

	match := cityReg.FindAllSubmatch(contents, -1)

	genderMatch := genderReg.FindAllSubmatch(contents, -1)

	for i, m := range match {
		url := string(m[1])
		name := string(m[2])
		gender := string(genderMatch[i][1])
		//rs.Items = append(rs.Items, "User " + name + " gender " + gender)
		rs.Requests = append(rs.Requests, engine.Request{
			Url: url,
			ParseFunc: func(contents []byte) engine.ParseResult {
				return ParseProfile(contents, url, name, gender)
			},
		})
	}

	// 更多城市及分页
	cityUrlMatch := cityUrlReg.FindAllSubmatch(contents, -1)
	for _, m := range cityUrlMatch {
		rs.Requests = append(rs.Requests, engine.Request{
			Url:       string(m[1]),
			ParseFunc: ParseCity,
		})
	}

	return rs
}
